package com.live.shop.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.live.shop.model.UserModel;

public class UserDatabase {
	private static MyDatabaseHelper helper;
	public UserDatabase(Context context){
		//创建数据库和表
		helper = new MyDatabaseHelper(context, "user.db",null,3,null);
	}
	@SuppressLint("Range")
	public static UserModel select(){
		UserModel model= new UserModel();
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from user_info where id = 1", null);
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				model.setUuid(cursor.getInt(cursor.getColumnIndex("uuid")));
				model.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
				model.setAccount(cursor.getString(cursor.getColumnIndex("account")));
				model.setPassword(cursor.getString(cursor.getColumnIndex("password")));
				model.setEndtime(cursor.getString(cursor.getColumnIndex("endtime")));
				model.setHeadurl(cursor.getString(cursor.getColumnIndex("headurl")));
				model.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
				model.setCommentKeep(cursor.getInt(cursor.getColumnIndex("comment_keep")));
				model.setPrologueInterval(cursor.getInt(cursor.getColumnIndex("prologue_interval")));
				model.setPcIp(cursor.getString(cursor.getColumnIndex("pc_ip")));
				model.setPrologueBreak(cursor.getInt(cursor.getColumnIndex("prologue_break")));
				model.setPrologueRestart(cursor.getInt(cursor.getColumnIndex("prologue_restart")));
				model.setProloguePlaytype(cursor.getInt(cursor.getColumnIndex("prologue_playtype")));
				model.setKeywordKeeptype(cursor.getInt(cursor.getColumnIndex("keyword_keeptype")));
				model.setFunctionId(cursor.getInt(cursor.getColumnIndex("function_id")));
				model.setFunctionEventCode(cursor.getInt(cursor.getColumnIndex("function_event_code")));
				model.setFunctionResid(cursor.getString(cursor.getColumnIndex("function_resid")));
				model.setFunctionPackage(cursor.getString(cursor.getColumnIndex("function_package")));
				model.setToken(cursor.getString(cursor.getColumnIndex("token")));
				model.setAliyunAccessKeyID(cursor.getString(cursor.getColumnIndex("aliyun_akid")));
				model.setAliyunAccessKeySecret(cursor.getString(cursor.getColumnIndex("aliyun_aksecret")));
				model.setAliyunAppkey(cursor.getString(cursor.getColumnIndex("aliyun_appkey")));
				model.setAliyunFontName(cursor.getString(cursor.getColumnIndex("aliyun_font_name")));
				model.setAliyunSpeedLevel(cursor.getString(cursor.getColumnIndex("aliyun_speed_level")));
				model.setAliyunVolume(cursor.getString(cursor.getColumnIndex("aliyun_volume")));
				model.setAliyunVoiceSelect(cursor.getString(cursor.getColumnIndex("aliyun_voice_select")));
				model.setAliyunToken(cursor.getString(cursor.getColumnIndex("aliyun_token")));
				model.setAliyunExpiredTime(cursor.getInt(cursor.getColumnIndex("aliyun_expired_time")));
			}
			cursor.close();
		}
		database.close();
		return model;
	}
	//更改阿里云Token
	public static void updateAliToken(UserModel model) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("aliyun_token", model.getAliyunToken());
		values.put("aliyun_expired_time", model.getAliyunExpiredTime());
		database.update("user_info", values, "id = ?", new String[] {String.valueOf(1)});
		values.clear();
		database.close();
	}
	//更改阿里云配置
	public static void updateAliKey(UserModel model) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("aliyun_akid", model.getAliyunAccessKeyID());
		values.put("aliyun_aksecret", model.getAliyunAccessKeySecret());
		values.put("aliyun_appkey", model.getAliyunAppkey());
		values.put("aliyun_font_name", model.getAliyunFontName());
		values.put("aliyun_speed_level", model.getAliyunSpeedLevel());
		values.put("aliyun_volume", model.getAliyunVolume());
		values.put("aliyun_voice_select", model.getAliyunVoiceSelect());
		database.update("user_info", values, "id = ?", new String[] {String.valueOf(1)});
		values.clear();
		database.close();
	}
	//更改头像
	public static void updateUserHeadurl(String headurl) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET headurl = '"+headurl+"' WHERE id = 1");
		database.close();
	}
	//恢复用户状态
	public static void restoreStatus() {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET status=1 WHERE id = 1");
		database.close();
	}
	//清除token
	public static void clearToken(int status) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET token = '',status="+status+" WHERE id = 1");
		database.close();
	}
	//更改登录账号
	@SuppressLint("Range")
	public static void updateUserData(int uuid, String account, String password, String token,String endtime, String nickname) {
		SQLiteDatabase database = helper.getReadableDatabase();
		//检测账号是否改变
		Cursor cursor = database.rawQuery("select * from user_info where id = 1", null);
		int oldUuid = 0;
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				oldUuid = cursor.getInt(cursor.getColumnIndex("uuid"));
			}
			cursor.close();
		}
		if(oldUuid != uuid){
			ContentValues values2 = new ContentValues();
			values2.put("headurl", "");
			values2.put("comment_keep", 0);
			values2.put("prologue_interval", 0);
			values2.put("pc_ip", "");
			values2.put("prologue_break", 0);
			values2.put("prologue_restart", 0);
			values2.put("prologue_playtype", 0);
			values2.put("keyword_keeptype", 0);
			values2.put("function_id", 0);
			values2.put("function_event_code", 0);
			values2.put("function_resid", "");
			values2.put("function_package", "");
			values2.put("aliyun_akid", "");
			values2.put("aliyun_aksecret", "");
			values2.put("aliyun_appkey", "");
			values2.put("aliyun_font_name", "");
			values2.put("aliyun_speed_level", "");
			values2.put("aliyun_volume", "");
			values2.put("aliyun_voice_select", "");
			values2.put("aliyun_token", "");
			values2.put("aliyun_expired_time", 0);
			database.update("user_info", values2, "id = ?", new String[] {String.valueOf(1)});
			values2.clear();
		}
		ContentValues values = new ContentValues();
		values.put("uuid", uuid);
		values.put("nickname", nickname);
		values.put("account", account);
		values.put("password", password);
		values.put("endtime", endtime);
		values.put("token", token);
		values.put("status", 1);
		database.update("user_info", values, "id = ?", new String[] {String.valueOf(1)});
		values.clear();
		database.close();
	}
	//更改应用平台
	public static void updateFunctionId(int function_id, int function_event_code, String function_resid, String function_package) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET function_id = "+function_id+",function_event_code = "+function_event_code+
				",function_resid = '"+function_resid+"' ,function_package = '"+function_package+"' WHERE id = 1");
		database.close();
	}
	//更改保存评论记录状态
	public static void updateCommentKeep(int comment_keep) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET comment_keep = "+comment_keep+" WHERE id = 1");
		database.close();
	}
	//更改保存评论记录状态
	public static void updateNickname(String nickname) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET nickname = '"+nickname+"' WHERE id = 1");
		database.close();
	}
	//直播话术间隔
	public static void updatePrologueInterval(int prologue_interval) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET prologue_interval = "+prologue_interval+" WHERE id = 1");
		database.close();
	}
	//电脑端IP
	public static void updatePcIp(String pc_ip) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET pc_ip = '"+pc_ip+"' WHERE id = 1");
		database.close();
	}

	//允许话术被公屏打断
	public static void updatePrologueBreak(int prologue_break) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET prologue_break = "+prologue_break+" WHERE id = 1");
		database.close();
	}
	//打断的话术是否从开头播放
	public static void updatePrologueRestart(int prologue_restart) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET prologue_restart = "+prologue_restart+" WHERE id = 1");
		database.close();
	}
	//话术播放方式 0轮播 1随机
	public static void updateProloguePlaytype(int prologue_playtype) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET prologue_playtype = "+prologue_playtype+" WHERE id = 1");
		database.close();
	}
	//关键词回复播放 0评论实时 1评论历史
	public static void updateKeywordKeeptype(int keyword_keeptype) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET keyword_keeptype = "+keyword_keeptype+" WHERE id = 1");
		database.close();
	}
	//新增一条
	public static void insert(int uuid,String account,String password,String token,String endtime, String nickname) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("uuid", uuid);
		values.put("nickname", nickname);
		values.put("account", account);
		values.put("password", password);
		values.put("endtime", endtime);
		values.put("token", token);
		database.insert("user_info", null, values); // 插入一条数据
		values.clear();
		database.close();
    }
	
}
